package com.jinke.api.modules.third.huaLei.model.response;

import com.alibaba.fastjson2.annotation.JSONField;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Optional;

/**
 * @projectName:
 * @title: HuaLeiCreateOrderResponse
 * @description: 华磊添加订单 响应体
 * @see:
 * @author: hhp
 * @version: 1.0.0
 * @createTime: 2024/8/29 14:49
 */
@Slf4j
@Data
public class HuaLeiCreateOrderResponse {
    private String ack;
    private String attr1;
    private String attr2;
    @JSONField(name = "channel_code")
    private String channelCode;
    private List<ChildItem> childList;
    @JSONField(name = "delay_type")
    private String delayType;
    @JSONField(name = "is_changenumbers")
    private String isChangeNumbers;
    /**
     * 是否延迟获取单号，Y代表需要延迟获取单号
     */
    @JSONField(name = "is_delay")
    private String isDelay;
    /**
     * 是否偏远，N为不偏远，Y为偏远，A为fedex偏远A级，B为fedex偏远B级, C为fedex偏远C级
     */
    @JSONField(name = "is_remote")
    private String isRemote;
    /**
     * 是否住宅，Y表示住宅地址
     */
    @JSONField(name = "is_residential")
    private String isResidential;
    @JSONField(name = "label_url")
    private String labelUrl;
    /**
     * 如果未获取到转单号，则该列存放失败原因(该内容请urldecode后查看)
     */
    private String message;
    /**
     * xxxxxxx，id需要保存，用于打印标签
     */
    @JSONField(name = "order_id")
    private String orderId;
    @JSONField(name = "order_privatecode")
    private String orderPrivateCode;
    /**
     * 特殊情况下使用，默认不用理会
     */
    @JSONField(name = "order_transfercode")
    private String orderTransferCode;
    @JSONField(name = "orderpricetrial_amount")
    private String orderPriceTrialAmount;
    @JSONField(name = "orderpricetrial_currency")
    private String orderPriceTrialCurrency;
    @JSONField(name = "post_customername")
    private String postCustomerName;
    /**
     * 值为3时需要调用获取跟踪号接口来更新单号
     */
    @JSONField(name = "product_tracknoapitype")
    private String productTrackNoApiType;
    /**
     * 参考号
     */
    @JSONField(name = "reference_number")
    private String referenceNumber;
    @JSONField(name = "return_address")
    private String returnAddress;
    /**
     * 跟踪号
     */
    @JSONField(name = "tracking_number")
    private String trackingNumber;
    @Data
    public static class ChildItem {
        /**
         * 子单号
         */
        @JSONField(name = "child_number")
        private String childNumber;
    }

    public String getMessage() {
        try {
            return URLDecoder.decode(Optional.ofNullable(message).orElse(""), StandardCharsets.UTF_8.name());
        } catch (UnsupportedEncodingException e) {
            log.error("decode message error:{}", e.getMessage());
            throw new RuntimeException(e);
        }
    }
}
